import 'package:enterprise_v6_app/app/core/core.dart';

///自定义进度条
class CustomProgressBar extends StatelessWidget {
  const CustomProgressBar({required this.percentage, super.key});

  final double percentage;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      height: 8.w,
      child: CustomPaint(
        painter: ProgressBarPainter(percentage),
        child: const Row(),
      ),
    );
  }
}

class ProgressBarPainter extends CustomPainter {
  ProgressBarPainter(this.percentage);

  double percentage;

  @override
  void paint(Canvas canvas, Size size) {
    final progressPaint = Paint()
      ..color = const Color(0xFF1869FF)
      ..style = PaintingStyle.fill;
    // 绘制进度条的背景
    final backgroundRect = Rect.fromPoints(const Offset(0.0, 0.0), Offset(size.width, size.height));
    final backgroundPaint = Paint()..color = const Color(0x80C1C7FF);
    canvas.drawRect(backgroundRect, backgroundPaint);
    // 绘制进度条的前景
    final progressWidth = size.width * percentage; // 进度条的宽度
    if (percentage >= 0.1 && size.width != progressWidth) {
      // 白色的背景
      final backgroundPaintWhite = Paint()..color = Colors.white;
      final Path pathWhite = Path()
        ..moveTo(0, size.height) // 左下角
        ..lineTo(progressWidth - 5.w, size.height) // 右下角
        ..lineTo(progressWidth + 5.w, 0) // 右上角
        ..lineTo(0, 0); // 左上角
      canvas.drawPath(pathWhite, backgroundPaintWhite);
      final Path path = Path()
        ..moveTo(0, size.height) // 左下角
        ..lineTo(progressWidth - 10.w, size.height) // 右下角
        ..lineTo(progressWidth, 0) // 右上角
        ..lineTo(0, 0); // 左上角
      canvas.drawPath(path, progressPaint);
    } else {
      final progressRect = Rect.fromPoints(const Offset(0.0, 0.0), Offset(progressWidth, size.height));
      canvas.drawRect(progressRect, progressPaint);
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}
